let convert = require('xml-js');
let xml =
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<note importance="high" logged="true">' +
    '    <title>Happy</title>' +
    '    <todo>Work</todo>' +
    '    <todo>Play</todo>' +
    '</note>';

/**
 * 拟打算将数据存储到sqlite数据库中，
 * 将整个json转存到数据库中，
 * 导出格式，将json转xml。
 * @type {string}
 */
let result1 = convert.xml2json(xml, {compact: true, spaces: 4});
/**
 *{"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},
 * "note":{"_attributes":{"importance":"high","logged":"true"},
 * "title":{"_text":"Happy"},"todo":[{"_text":"Work"},{"_text":"Play"}]}}
 */
// let result2 = convert.xml2json(xml, {compact: false, spaces: 4});
/**
 * {"declaration":{"attributes":{"version":"1.0","encoding":"utf-8"}},"elements":[{"type":"element","name":"note","attributes":
 * {"importance":"high","logged":"true"},"elements":[{"type":"element","name":"title",
 * "elements":[{"type":"text","text":"Happy"}]},{"type":"element","name":
 * "todo","elements":[{"type":"text","text":"Work"}]},{"type":"element","name":"todo","elements":[{"type":"text","text":"Play"}]}]}]}
 */
let t = convert.json2xml(result1,{compact: true, spaces: 4})
// console.log(t);

//读取xml文本
let xml2 = require('fs').readFileSync('../data/test.xml', 'utf8');
let options = {compact: true, spaces: 2};
let result = convert.xml2js(xml2, options); // or convert.xml2json(xml, options)
console.log(JSON.stringify(result));